const app = getApp();
const utils = require('../../utils/util.js');
Component({
  /**
   * 自定义底部导航组件(名片首页)
   * 组件的属性列表
   */
  properties: {
    currPagePath: {
      type: String,
      value: '/pages/index/index',
      createUid: 0,
      isSelf: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    color: '#8a8a8a',
    selectedColor: '#FB8524',
    TabbarList: []
  },
  ready: function () {
    var timer = setInterval(function (app, page) {
      let isFriendState = app.globalData.isFriendState;
      if (isFriendState == 1) {
        let friendUid = app.globalData.friendUid;
        page.getStaffByUid(friendUid);
        clearInterval(timer);
      } else {
        page.getStaff(0);
        clearInterval(timer);
      }
    }, 1000, app, this)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    _initTabBarPages: function () {
      var that = this;
      var newTabbar = [];
      var isExists = false;
      that.data.TabbarList.forEach((item) => {
        item.isActive = item.pagePath == that.data.currPagePath;
        if (item.display) {
          newTabbar.push(item);
        }
        if(item.display && item.pagePath == that.data.currPagePath){
          isExists = true;
        }
      })
      that.setData({
        TabbarList: newTabbar
      })

      // detail对象，提供给事件监听函数
      this.triggerEvent('tabbarevent', {
        newTabbar: newTabbar
      })
      
      //页面不存在，跳转至首页
      if(!isExists){
        console.log('页面不存在,跳转至首页:' + wx.getStorageSync('showIndex'))
        wx.reLaunch({
          url:  wx.getStorageSync('showIndex')
        })
      }
    },

    //默认导航列表
    getDefaultList: function(){
     return [{
        id: 0,
        pagePath: '/pages/index/index',
        text: '首页',
        iconPath: '/images/tabbar/20.png',
        selectedIconPath: '/images/tabbar/19.png',
        iCount: 0,
        isActive: true,
        target: 'relaunch',
        KeyCode:'AIHOME'
      },
      {
        id: 1,
        pagePath: '/pages/classes-new/index',
        text: '产品',
        iconPath: '/images/tabbar/22.png',
        selectedIconPath: '/images/tabbar/21.png',
        iCount: 0,
        isActive: false,
        target: 'relaunch',
        KeyCode:'BUSINESS'
      },
      {
        id: 6,
        pagePath: '/merchant/merchants/join/index',
        text: '云加盟',
        iconPath: '/images/tabbar/3.png',
        selectedIconPath: '/images/tabbar/4.png',
        iCount: 0,
        isActive: false,
        target: 'relaunch',
        KeyCode:'JOIN'
      },
      {
        id: 7,
        pagePath: '/merchant/supply/list/index',
        text: '云点单',
        iconPath: '/images/tabbar/33.png',
        selectedIconPath: '/images/tabbar/34.png',
        iCount: 0,
        isActive: false,
        target: 'relaunch',
        KeyCode:'DIANDAN'
      },
      {
        id: 2,
        pagePath: '/merchant/merchants/nearbyCoupon/index',
        text: '9.9',
        iconPath: '/images/tabbar/32.png',
        selectedIconPath: '/images/tabbar/31.png',
        iCount: 0,
        isActive: false,
        target: 'relaunch',
        KeyCode:'COUPON'
      },
      {
        id: 3,
        pagePath: '/pages/about/index',
        text: '官网',
        iconPath: '/images/tabbar/26.png',
        selectedIconPath: '/images/tabbar/25.png',
        iCount: 0,
        isActive: false,
        target: 'relaunch',
        KeyCode:'COMPANY'
      },
      {
        id: 4,
        pagePath: '/pages/weijieke/states/index',
        text: '商圈',
        iconPath: '/images/tabbar/24.png',
        selectedIconPath: '/images/tabbar/23.png',
        iCount: 0,
        isActive: false,
        target: 'relaunch',
        KeyCode:'SUBJECT'
      },
      {
        id: 5,
        pagePath: '/merchant/merchants/myCardCoupon/index',
        text: '我的',
        iconPath: '/images/tabbar/29.png',
        selectedIconPath: '/images/tabbar/30.png',
        iCount: 0,
        isActive: false,
        target: 'relaunch',
        KeyCode:'MY'
      }
      ]
    },

    // 单击tab触发的函数
    onTabbarItemTap(ev) {
      let key = 0;
      if (ev.detail.target) {
        key = ev.detail.target.dataset.key;
      } else {
        key = ev.currentTarget.dataset.key;
      }
      var link = this.data.TabbarList[key];
      var target = link.target.toLowerCase();
      console.log(this.data.isSelf + ' # ' + this.data.createUid)
      console.log(app.globalData.staff)
      let path = link.pagePath;
      if(link.KeyCode=='DIANDAN'){
        let model = this.data.isSelf ? 1 : 2;
        path += '?createUid=' + this.data.createUid +'&model=' + model + '&version=5';
      }
      utils.subscribe();
      console.log(path)
      switch(target){
        case 'relaunch': wx.reLaunch({url: path}); break;
        case 'navigateto': wx.navigateTo({url: path}); break;
        case 'redirectto': wx.redirectTo({url: path}); break;
      }
    },

    //获取名片底部导航
    getStaffTabbars: function (isSelf) {
      var that = this;
      var cardId = wx.getStorageSync('cardId');     
      var TabbarList = that.getDefaultList();             
      wx.request({
        url: app.globalData.host + '/api/AddressBook/GetStaffTabbars?staffId=' + cardId,
        success: function (res) {
          if (res.data.Code == 0) {
            if (res.data.Data) {
              var list = res.data.Data;
              var showIndex='';
              var newList = [];
              for (var i = 0; i <TabbarList.length; i++) {                 
                var item = TabbarList[i];
                for(var j = 0;j <list.length; j++){
                  if(TabbarList[i].KeyCode == list[j].KeyCode){
                    item.text = list[j].UserName ? list[j].UserName : list[j].Name;
                    item.isActive = item.pagePath == that.data.currPagePath;
                    item.display =  list[j].Display;                    
                    if (item.KeyCode == 'AIHOME' && isSelf) {                      
                      item.display = true;                      
                    }       
                    if(item.display && showIndex ==''){
                      showIndex = item.pagePath;
                    }           
                    break;
                  }
                }
                if(item.display){
                  newList.push(item);
                }
              }
              wx.setStorageSync('showIndex', showIndex);
              that.data.TabbarList = newList;
              that._initTabBarPages();             
            }
          }
        },
      })     
    },
    getStaffByUid: function(uid){
      var that = this;
      var cardId = wx.getStorageSync('cardId');
      if(uid == 0 && app.globalData.userInfo){
        uid = app.globalData.userInfo.Uid;
      }      
      if(cardId && uid > 0){
        wx.request({
          url: app.globalData.host + '/api/staff/GetStaffIdByUid?uid=' + uid,
          success: function (res) {            
            if (res.data.Code == 0) {   
              that.data.createUid = res.data.Data.Uid
              that.data.isSelf = res.data.Data.Id == cardId ? true : false; //是否本人名片
              that.getStaffTabbars(that.data.isSelf);
            }
          },
        })   
      }
      else{
        that.getStaffTabbars(false);
      }
    },
    getStaff: function(){
      var that = this;
      var cardId = wx.getStorageSync('cardId');
      if(cardId){
        utils.getStaff((staff)=>{
          that.data.createUid = staff.Uid;
          that.data.isSelf = staff.Uid == app.globalData.userInfo.Uid ? true : false; //是否本人名片
          that.getStaffTabbars(that.data.isSelf);
        });
      }
      else{
        that.getStaffTabbars(false);
      }
    }
  },
 
})